package sandbox.util;

import org.springframework.data.jpa.repository.JpaRepository;

public class RepoHelper {

    public static <T> T findObject(Class<T> clazz, JpaRepository<T, Long> repo, Long id) throws Exception {
        if(repo == null) {
            throw new Exception("Repository must not be null");
        }
        T obj = repo.findOne(id);
        if(obj == null) {
            throw new Exception("Couldn't find object of type '" + clazz.getSimpleName() + "' with id " + id);
        }
        return obj;
    }

    public static Object find(Class clazz, JpaRepository<?, Long> repo, Long id) throws Exception {
        return findObject(clazz, repo, id);
    }
}
